package com.lkm.app_ui.m.Imple.fun;

import com.google.gson.annotations.SerializedName;
import com.lkm.app.event.OnNetworkStateEvent;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppHelp;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.help.PhoneInfoUtils;
import com.lkm.app_ui.help.WifiUtils;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.market2_0.entity.ResponEntity;

import java.util.Map;



class UpSteelyardRegTask extends TaskAdapter<Void, Void, ResponEntityAbs<Void>> {


    public UpSteelyardRegTask() {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), UpSteelyardRegTask.class.getName(), AppConst.getTaskManager2());
    }

    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);

        mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
            @Override
            public void run() {
                ayncdoExecuting();
            }
        });

    }

    private void ayncdoExecuting() {

        SteelyardRegTO steelyardRegTO = new SteelyardRegTO();

        steelyardRegTO.balance_no = RunTimeData.steelyardInfo.getSteelyardMac();
        steelyardRegTO.mark_address = RunTimeData.organizationInfo.getAddress();
        steelyardRegTO.mark_phone = RunTimeData.organizationInfo.getContactPhone();
        steelyardRegTO.mark_name = RunTimeData.organizationInfo.getName();
        steelyardRegTO.mark_user= RunTimeData.organizationInfo.getContactMan();

        steelyardRegTO.stall_license = RunTimeData.steelyardInfo.getStallEntity().businessentity;
        steelyardRegTO.stall_license_no = RunTimeData.steelyardInfo.getStallEntity().creditcode;
        steelyardRegTO.stall_name = RunTimeData.steelyardInfo.getStallEntity().getStallName();
        steelyardRegTO.stall_no = RunTimeData.steelyardInfo.getStallEntity().getStallNo();
        steelyardRegTO.stall_phone = RunTimeData.steelyardInfo.getStallEntity().getPhone();
        steelyardRegTO.stall_type = RunTimeData.steelyardInfo.getStallEntity().type;
        steelyardRegTO.stall_user = RunTimeData.steelyardInfo.getStallEntity().getName();
        steelyardRegTO.exit_factory_id = RunTimeData.steelyardInfo.b_id;

        steelyardRegTO.mac_address = WifiUtils.getInstance().getLocalMacAddress();

        steelyardRegTO.app_v = AppHelp.getLocalVersionName();
        try {
            steelyardRegTO.ccid = new PhoneInfoUtils(AppConst.getApplication()).getIccid();
        }catch (Exception e){}

        Map<String, Object> stringStringMap = GsonHelp.fromJson(GsonHelp.toJson(steelyardRegTO),new com.google.gson.reflect.TypeToken<Map<String, String>>() {}.getType());

        String json = AppApi.upSteelyardRegTask(stringStringMap,this);

        ResponEntity<Void> responEntity = ResponEntity.fromJson(json, null, "");




        if (responEntity.isSuccess()) {
            complete(new ResponEntity<Void>().success(null, responEntity.getMsg()));

        } else {


            if(json == null){
                requestRe();
            }else {
                complete(new ResponEntity<Void>().fail(null, responEntity.getMsg()));
            }
        }


    }


    int isReForNetOK = 0;
    private void requestRe() {
        if (!getIsContinue()) return;

        if(isReForNetOK>=5){
            complete(new ResponEntity<Void>().fail(null, ""));
            return;
        }

        if (NetworkStateModule.getInstance().getIsNetworkOK()) {
            isReForNetOK ++;
            doExecuting(null);
        } else {

            if (observerAutoNext != null)
                LinkBus.unregisterReceiver(observerAutoNext);
            LinkBus.registerReceiver(getobserverAutoNext());
        }
    }

    ObserverAutoNext observerAutoNext;

    private ObserverAutoNext getobserverAutoNext() {
        if (observerAutoNext == null) {
            observerAutoNext = new ObserverAutoNext<OnNetworkStateEvent>() {
                @Override
                public void onReceiver(OnNetworkStateEvent data) {
                    if (!getIsContinue()) {
                        LinkBus.unregisterReceiver(this);
                        return;
                    }
                    if (data.extIsNetworkOK) {
                        LinkBus.unregisterReceiver(this);
                        doExecuting(null);
                    }
                }

                @Override
                public Class getBusEventClass() {
                    return OnNetworkStateEvent.class;
                }
            };
        }
        return observerAutoNext;
    }

    @Override
    public boolean onInterceptEnd(boolean isCancel, ResponEntityAbs<Void> result) {
        if (observerAutoNext != null)
            LinkBus.unregisterReceiver(observerAutoNext);
        return super.onInterceptEnd(isCancel, result);
    }


    private static class SteelyardRegTO {

        @SerializedName("balance_no")
        public String balance_no;

        @SerializedName("stall_no")
        public String stall_no;

        @SerializedName("stall_name")
        public String stall_name;

        @SerializedName("stall_license")
        public String stall_license;

        @SerializedName("stall_license_no")
        public String stall_license_no;

        @SerializedName("stall_type")
        public String stall_type;

        @SerializedName("stall_user")
        public String stall_user;

        @SerializedName("stall_phone")
        public String stall_phone;

        @SerializedName("mark_user")
        public String mark_user;

        @SerializedName("mark_name")
        public String mark_name;

        @SerializedName("mark_address")
        public String mark_address;

        @SerializedName("mark_phone")
        public String mark_phone;

        @SerializedName("mark_license")
        public String mark_license;

        @SerializedName("mark_license_no")
        public String mark_license_no;

        @SerializedName("exit_factory_id")
        public String exit_factory_id;

        @SerializedName("mac_address")
        public String mac_address;

        @SerializedName("ccid")
        public String ccid;

        @SerializedName("app_v")
        public String app_v;
    }
}
